package com.gwtintegration.smartrf.client;

import com.google.gwt.core.client.JavaScriptObject;

public class ConverterCache {

	private JavaScriptObject cache;

	public ConverterCache() {
		cache = JavaScriptObject.createArray();
	}

	public native void add(Object src, Object dest)
	/*-{
	 	var converterCache = this.@com.gwtintegration.smartrf.client.ConverterCache::cache;
	 	
	 	src.__converterCache = converterCache;
		src.__converterIndex = converterCache.length;
	 	
	 	converterCache.push(dest);
	 }-*/;

	public native Object get(Object src)
	/*-{
		var converterCache = this.@com.gwtintegration.smartrf.client.ConverterCache::cache;
		
		if (src.__converterCache === converterCache && src.__converterIndex !== undefined) {
			return converterCache[src.__converterIndex];
		}
		
		return null;
	 }-*/;

	public native boolean contains(Object src)
	/*-{
		var converterCache = this.@com.gwtintegration.smartrf.client.ConverterCache::cache;
		return src.__converterCache === converterCache && src.__converterIndex !== undefined;
	 }-*/;

	public native void clear()
	/*-{
	 	var converterCache = this.@com.gwtintegration.smartrf.client.ConverterCache::cache;
	 	converterCache.splice(0, converterCache.length);
	 }-*/;

}
